package com.feicheng.iou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.entity.Role;
import com.feicheng.iou.entity.RolePermission;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.mapper.RoleMapper;
import com.feicheng.iou.mapper.RolePermissionMapper;
import com.feicheng.iou.pojo.login.AddRolePOJO;
import com.feicheng.iou.pojo.login.RolePermissionPOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private RolePermissionPOJO rolePermissionPOJO;


    /**
     * @Author wuweinan
     * @Description 查询用户对应的角色列表
     * @Date 2021-7-23 10:45
     * @param roleIds
     * @return  List<Integer>
     */
    @Override
    public List<Role> selectRoles(List<Integer> roleIds) {
        log.info("RoleServiceImpl的selectRoles方法进入，参数roleIds：" + roleIds);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id", roleIds).select("role_id","role_name");
        return roleMapper.selectList(queryWrapper);
    }

    /**
     * @Author wuweinan
     * @Description //查询出所有的角色
     * @Date 2021-7-23 10:45
     * @param
     * @return  ResultInfo
     */
    @Override
    public ResultInfo selectAllRoles() {
        log.info("RoleServiceImpl的selectAllRoles方法进入");
        List<Role> roles = roleMapper.selectList(null);
        return ResultInfo.success(roles);
    }

    /**
     * @Author wuweinan
     * @Description  //根据RoleId查询出角色信息和其权限集合
     * @Date 2021-7-23 10:45
     * @param roleId
     * @return  ResultInfo
     */
    @Override
    public ResultInfo selectRoleByRoleId(Integer roleId) {
        log.info("RoleServiceImpl的selectAllRoles方法进入,参数RoleId为:"+roleId);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        Role role = roleMapper.selectOne(queryWrapper);

        QueryWrapper<RolePermission> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("role_id", roleId);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper1);

        rolePermissionPOJO.setRole(role);
        rolePermissionPOJO.setRolePermissionList(rolePermissions);

        return ResultInfo.success(rolePermissionPOJO);
    }


    /**
     * @Author wuweinan
     * @Description  //新增角色及其权限
     * @Date 2021-7-23 10:45
     * @param
     * @return ResultInfo
     */
    @Override
    public ResultInfo insertRole(AddRolePOJO addRolePOJO) {
        log.info("RoleServiceImpl的selectAllRoles方法进入,参数addRolePOJO为:"+addRolePOJO);
        //新增角色信息
        Integer integer = roleMapper.insertRole(addRolePOJO);

        if(integer != 1){
            throw new SystemException(ReturnCode.INSERT_ROLE_FAIL);
        }

        //新增角色的权限
        Integer integer1 = rolePermissionMapper.
                insertRolePermission(addRolePOJO.getRole().getRoleId(), addRolePOJO.getPermissionIds());

        if(integer1 == 0){
            throw new SystemException(ReturnCode.INSERT_PERMISSION_FAIL);
        }

        return ResultInfo.success();
    }
}
